50) $limit = 50;
// Contexto (tu versión desactiva verificación SSL; lo dejo igual por compatibilidad)
$context = stream_context_create([
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false
],
"http" => [
"timeout" => 12,
"user_agent" => "Mozilla/5.0 (compatible; RSS-Reader/2.0)"
]
]);
$rssContent = @file_get_contents($rssUrl, false, $context);
if (!$rssContent) {
echo json_encode(["error" => "No se pudo cargar el RSS"], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit;
}
$rss = @simplexml_load_string($rssContent, "SimpleXMLElement", LIBXML_NOCDATA);
if (!$rss || !isset($rss->channel->item)) {
echo json_encode(["error" => "RSS inválido"], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit;
}
$noticias = [];
$count = 0;
foreach ($rss->channel->item as $item) {
if ($count >= $limit) break;
$titulo = mb_strimwidth(trim((string)$item->title), 0, 120, "…", "UTF-8");
// contenido (content:encoded si está)
$contentEncoded = null;
$childrenContent = $item->children("content", true);
if ($childrenContent && isset($childrenContent->encoded)) {
$contentEncoded = (string)$childrenContent->encoded;
}
$contenidoCompleto = $contentEncoded ?: (string)($item->description ?? "");
// Imagen: media:content, o
$imagen = "https://via.placeholder.com/400x250/121212/00bcd4?text=LaVoz";
$childrenMedia = $item->children("media", true);
if ($childrenMedia && isset($childrenMedia->content)) {
$attrs = $childrenMedia->content->attributes();
if ($attrs && isset($attrs["url"])) $imagen = (string)$attrs["url"];
} elseif (preg_match('/
]+src="([^"]+)"/i', $contenidoCompleto, $m)) {
$imagen = $m[1];
}
if (!preg_match('/^https?:\/\//i', $imagen)) {
$imagen = "https:" . $imagen;
}
// desc corta
$desc = trim(preg_replace('/\s+/', ' ', strip_tags($contenidoCompleto)));
if (mb_strlen($desc, "UTF-8") < 50 && isset($item->description)) {
$desc = trim(preg_replace('/\s+/', ' ', strip_tags((string)$item->description)));
}
$desc = mb_strimwidth($desc, 0, 180, "…", "UTF-8");
// fecha
$fechaRaw = (string)($item->pubDate ?? "");
$timestamp = $fechaRaw ? strtotime($fechaRaw) : false;
$fecha = $timestamp ? date("d/m/Y H:i", $timestamp) : "Fecha no disponible";
$noticias[] = [
"titulo" => $titulo,
"descripcion" => $desc,
"descripcionCompleta" => trim(preg_replace('/\s+/', ' ', strip_tags($contenidoCompleto))),
"imagen" => $imagen,
"link" => (string)($item->link ?? ""),
"fecha" => $fecha
];
$count++;
}
echo json_encode($noticias, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);